package com.vcredit.bird.service.product;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.core.type.TypeReference;
import com.vcredit.bird.api.ICreditProductService;
import com.vcredit.bird.util.CommonUtil;
import com.vcredit.bird.vo.product.CreditSubmitDto;
import com.vcredit.bird.vo.product.ProductDecisionResultDto;
import com.vcredit.creditcat.exception.ProductCanNotApplyException;
import com.vcredit.creditcat.vo.CommonResultExt;
import com.vcredit.framework.annotation.Log;
import com.vcredit.framework.util.HttpRequest;
import com.vcredit.framework.util.JacksonUtil;

@Service("creditFlowerService")
public class CreditFlowerService implements ICreditProductService {
  @Value("${creditFlower.accede}")
  private String creditFlowerUrl;

  @Log
  @Override
  public Object commit(Object submitDto) throws ProductCanNotApplyException, Exception {
    // 调用申请接口
    // String url = "http://10.138.60.143:10000/api/custProductSimulator/receiveCreditItem";

    String resultJson =
        HttpRequest.doJson(creditFlowerUrl.concat("/decision"), JacksonUtil.Obj2Json(submitDto));
    CommonResultExt<CreditSubmitDto> result =
        JacksonUtil.Json2Obj(resultJson, new TypeReference<CommonResultExt<CreditSubmitDto>>() {});
    if (!"000".equals(result.getStatus())) {
      throw new ProductCanNotApplyException(result.getMsg());
    }
    return null;
  }

  @Override
  public Object checkCredit(Object submitDto) throws ProductCanNotApplyException, Exception {
    // Boolean resultBoolean = false;
    // //String url = "http://localhost:8082/creditcat-api/custProductSimulator/receiveIsBack";
    // String result =
    // HttpRequest.doJson(creditFlowerUrl.concat("/accessible"),JacksonUtil.Obj2Json(parmDto));
    // if (CommonUtil.validJsonString(result)) {
    // CommonResultExt<Boolean> json2Obj = JacksonUtil.Json2Obj(result, new
    // TypeReference<CommonResultExt<Boolean>>() {
    // });
    // if (ObjectUtils.equals(null, json2Obj)) {
    // throw new ProductCanNotApplyException("Flower isBlack resove error.");
    // }
    // if (!"000".equals(json2Obj.getStatus())) {
    // throw new ProductCanNotApplyException(json2Obj.getMessage());
    // }
    // resultBoolean = !(json2Obj.getData());
    // }
    // return resultBoolean;
    return null;
  }

  @Log
  @Override
  public Object getCredit(Object submitDto) throws Exception {
    ProductDecisionResultDto resultDto = null;
    // String url = "http://localhost:8082/creditcat-api/custProductSimulator/receiveQueryLimit";
    String jsonParamString = submitDto.toString();
    String result = HttpRequest.doJson(creditFlowerUrl.concat("/inquiry"), jsonParamString);
    if (CommonUtil.validJsonString(result)) {

      CommonResultExt<ProductDecisionResultDto> json2Obj =
          JacksonUtil.Json2Obj(result,
              new TypeReference<CommonResultExt<ProductDecisionResultDto>>() {});
      if (!"000".equals(json2Obj.getStatus())) {
        throw new ProductCanNotApplyException(json2Obj.getMsg());
      }
      resultDto = json2Obj.getData();

    }
    return resultDto;
  }

  //  @Override
  //  public void noticeApplyResult(ProductDecisionResultDto paramDto)
  //      throws ProductCanNotApplyException, Exception {
  //    String resultJson =
  //        HttpRequest.doJson(productAsync.concat("/creditFlower/noticeApplyResult"),
  //            JacksonUtil.Obj2Json(paramDto));
  //    CommonResultExt<String> result =
  //        JacksonUtil.Json2Obj(resultJson, new TypeReference<CommonResultExt<String>>() {});
  //    if (!"000".equals(result.getStatus())) {
  //      throw new ProductCanNotApplyException(result.getMessage());
  //    }
  //  }

}
